* DHS Calendar Tutorial - Example 1. * Basic string manipulation. * download the model dataset for individual women's recode: "ZZIR62FL.SAV" * the model datasets are available at http://dhsprogram.com/data/download-model-datasets.cfm . * change to a working directory where the data are stored * or add the full path to the 'get file' command below. cd "C:\Data\DHS_model". * open the dataset, and just keep the variables we are going to use. get file="ZZIR62FL.SAV" / keep vcal$1 v000 v005 v007 v008 v017 v018 v019. * 1) display column 1 of the calendar for the first 6 respondents. list variables = vcal$1 /cases from 1 to 5. List Notes |---------------------------------------------|-------------------------------------------| |Output Created |31-AUG-2017 00:56:43 | |---------------------------------------------|-------------------------------------------| |Comments | | |--------------|------------------------------|-------------------------------------------| |Input |Filter | | | |------------------------------|-------------------------------------------| | |Weight | | | |------------------------------|-------------------------------------------| | |Split File | | | |------------------------------|-------------------------------------------| | |N of Rows in Working Data File|8348 | |---------------------------------------------|-------------------------------------------| |Syntax |list variables = vcal$1 /cases from 1 to 5.| |--------------|------------------------------|-------------------------------------------| |Resources |Processor Time |00:00:00.36 | | |------------------------------|-------------------------------------------| | |Elapsed Time |00:00:00.35 | |-----------------------------------------------------------------------------------------| VCAL$1 00000BPPPPPPPP00000000000000000000000BPPPPPPPP00000000000000000000 PPPPPP000000000000000000000000BPPPPPPPP000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000 0000000000BPPPPPPPP00000000000BPPPPPPPP000000000000000000000000000 0BPPPPPPPP000000000000000000000000BPPPPPPPP00000000000000000000000 Number of cases read: 5 Number of cases listed: 5 * 2) calculate the full length of calendar by displaying length of strings. compute vcal_len = char.length(vcal$1). variable labels vcal_len "Length of calendar". print formats vcal_len (F2.0). list variables = vcal_len /cases from 1 to 5. List Notes |---------------------------------------------|---------------------------------------------| |Output Created |31-AUG-2017 00:56:43 | |---------------------------------------------|---------------------------------------------| |Comments | | |--------------|------------------------------|---------------------------------------------| |Input |Filter | | | |------------------------------|---------------------------------------------| | |Weight | | | |------------------------------|---------------------------------------------| | |Split File | | | |------------------------------|---------------------------------------------| | |N of Rows in Working Data File|8348 | |---------------------------------------------|---------------------------------------------| |Syntax |list variables = vcal_len /cases from 1 to 5.| |--------------|------------------------------|---------------------------------------------| |Resources |Processor Time |00:00:00.30 | | |------------------------------|---------------------------------------------| | |Elapsed Time |00:00:00.30 | |-------------------------------------------------------------------------------------------| vcal_len 80 80 80 80 80 Number of cases read: 5 Number of cases listed: 5 * 3) take a piece of a string from column 1. string piece (A12). compute piece = char.substr(vcal$1,44,12). variable labels piece "Piece of calendar". print formats piece (A12). list variables = piece /cases from 1 to 5. List Notes |---------------------------------------------|------------------------------------------| |Output Created |31-AUG-2017 00:56:43 | |---------------------------------------------|------------------------------------------| |Comments | | |--------------|------------------------------|------------------------------------------| |Input |Filter | | | |------------------------------|------------------------------------------| | |Weight | | | |------------------------------|------------------------------------------| | |Split File | | | |------------------------------|------------------------------------------| | |N of Rows in Working Data File|8348 | |---------------------------------------------|------------------------------------------| |Syntax |list variables = piece /cases from 1 to 5.| |--------------|------------------------------|------------------------------------------| |Resources |Processor Time |00:00:00.33 | | |------------------------------|------------------------------------------| | |Elapsed Time |00:00:00.32 | |----------------------------------------------------------------------------------------| piece 00000000BPPP 0BPPPPPPPP00 000000000000 0BPPPPPPPP00 00000BPPPPPP Number of cases read: 5 Number of cases listed: 5 * 4) find the position of a substring within a string. compute pos = char.index(vcal$1,"P"). variable labels pos "Position in calendar". print formats pos (F2.0). list variables = pos /cases from 1 to 5. List Notes |---------------------------------------------|----------------------------------------| |Output Created |31-AUG-2017 00:56:44 | |---------------------------------------------|----------------------------------------| |Comments | | |--------------|------------------------------|----------------------------------------| |Input |Filter | | | |------------------------------|----------------------------------------| | |Weight | | | |------------------------------|----------------------------------------| | |Split File | | | |------------------------------|----------------------------------------| | |N of Rows in Working Data File|8348 | |---------------------------------------------|----------------------------------------| |Syntax |list variables = pos /cases from 1 to 5.| |--------------|------------------------------|----------------------------------------| |Resources |Processor Time |00:00:00.02 | | |------------------------------|----------------------------------------| | |Elapsed Time |00:00:00.02 | |--------------------------------------------------------------------------------------| pos 21 15 0 26 17 Number of cases read: 5 Number of cases listed: 5 * 5) reverse a string * macro to reverse a string. define !ReverseStr(!positional !tokens(1) /!positional !tokens(1)) * first parameter is old variable, second is new variable. compute !2 = !1. string #a (A1). compute #l = length(rtrim(!2)). loop #i = 1 to #l/2. + compute #j = #l - #i + 1. + compute #a = char.substr(!2,#i,1). + compute substr(!2,#i,1) = char.substr(!2,#j,1). + compute substr(!2,#j,1) = #a. end loop. execute. !enddefine. * reverse a string. string rev_cal (a80). * reverse vcal$1 into rev_cal. !ReverseStr vcal$1 rev_cal variable labels rev_cal "Reversed calendar". print formats rev_cal (A80). list variables = rev_cal /cases from 1 to 5. List Notes |---------------------------------------------|--------------------------------------------| |Output Created |31-AUG-2017 00:56:44 | |---------------------------------------------|--------------------------------------------| |Comments | | |--------------|------------------------------|--------------------------------------------| |Input |Filter | | | |------------------------------|--------------------------------------------| | |Weight | | | |------------------------------|--------------------------------------------| | |Split File | | | |------------------------------|--------------------------------------------| | |N of Rows in Working Data File|8348 | |---------------------------------------------|--------------------------------------------| |Syntax |list variables = rev_cal /cases from 1 to 5.| |--------------|------------------------------|--------------------------------------------| |Resources |Processor Time |00:00:00.02 | | |------------------------------|--------------------------------------------| | |Elapsed Time |00:00:00.01 | |------------------------------------------------------------------------------------------| rev_cal 00000000000000000000PPPPPPPPB00000000000000000000000PPPPPPPPB00000 000000000000000000000000000PPPPPPPPB000000000000000000000000PPPPPP 000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000PPPPPPPPB00000000000PPPPPPPPB0000000000 00000000000000000000000PPPPPPPPB000000000000000000000000PPPPPPPPB0 Number of cases read: 5 Number of cases listed: 5 * 6) trim a string of leading and trailing spaces. string trim_cal (a80). compute trim_cal = rtrim(ltrim(vcal$1)). variable labels trim_cal "Trimmed calendar". print formats trim_cal (A80). list variables = trim_cal /cases from 1 to 5. List Notes |---------------------------------------------|---------------------------------------------| |Output Created |31-AUG-2017 00:56:44 | |---------------------------------------------|---------------------------------------------| |Comments | | |--------------|------------------------------|---------------------------------------------| |Input |Filter | | | |------------------------------|---------------------------------------------| | |Weight | | | |------------------------------|---------------------------------------------| | |Split File | | | |------------------------------|---------------------------------------------| | |N of Rows in Working Data File|8348 | |---------------------------------------------|---------------------------------------------| |Syntax |list variables = trim_cal /cases from 1 to 5.| |--------------|------------------------------|---------------------------------------------| |Resources |Processor Time |00:00:00.05 | | |------------------------------|---------------------------------------------| | |Elapsed Time |00:00:00.04 | |-------------------------------------------------------------------------------------------| trim_cal 00000BPPPPPPPP00000000000000000000000BPPPPPPPP00000000000000000000 PPPPPP000000000000000000000000BPPPPPPPP000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000 0000000000BPPPPPPPP00000000000BPPPPPPPP000000000000000000000000000 0BPPPPPPPP000000000000000000000000BPPPPPPPP00000000000000000000000 Number of cases read: 5 Number of cases listed: 5 * 7) display the length of calendar actually used, from the trimmed version. compute vcal_used = char.length(trim_cal). variable labels vcal_used "Length of calendar used". print formats vcal_used (F2.0). * should be the same as v019. list variables = vcal_used v019 /cases from 1 to 5. List Notes |---------------------------------------------|---------------------------------------------------| |Output Created |31-AUG-2017 00:56:44 | |---------------------------------------------|---------------------------------------------------| |Comments | | |--------------|------------------------------|---------------------------------------------------| |Input |Filter | | | |------------------------------|---------------------------------------------------| | |Weight | | | |------------------------------|---------------------------------------------------| | |Split File | | | |------------------------------|---------------------------------------------------| | |N of Rows in Working Data File|8348 | |---------------------------------------------|---------------------------------------------------| |Syntax |list variables = vcal_used v019 /cases from 1 to 5.| |--------------|------------------------------|---------------------------------------------------| |Resources |Processor Time |00:00:00.05 | | |------------------------------|---------------------------------------------------| | |Elapsed Time |00:00:00.05 | |-------------------------------------------------------------------------------------------------| vcal_used V019 66 66 66 66 66 66 66 66 66 66 Number of cases read: 5 Number of cases listed: 5